package com.mylibraryapplication.activity;


import android.util.Log;
import android.widget.TextView;
import com.mylibrary.util.Tools;
import com.mylibraryapplication.R;
import com.mylibraryapplication.base.BaseActivity;
import com.mylibraryapplication.http.Api;
import com.mylibraryapplication.http.Benefit;
import com.mylibraryapplication.http.RxHelper;
import com.mylibraryapplication.http.RxRetrofitCache;
import com.mylibraryapplication.utils.RxTest;
import com.mylibraryapplication.view.TitleView;
import butterknife.Bind;
import rx.Observable;

/**
 * RxJava网络请求
 */
public class RetrofitActivity extends BaseActivity {

    private static final String TAG = "RetrofitActivity";
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.text)
    TextView text;
    @Override
    public int getLayoutId() {
        return R.layout.retrofit;
    }

    @Override
    public void initView() {
        setTitle(title,"Http请求");
        Log.e(TAG,"测试增量编译");


//        Observable<Benefit> fromNetwrok=Api.getDefault().rxBenefits().compose(RxHelper.<Benefit>handleResult());
//        RxRetrofitCache.load(this, "cacheKey", 10 * 60 * 60,fromNetwrok,false)
//                .subscribe(new RxSubscribe<Benefit>(this) {
//                    @Override
//                    protected void _onNext(Benefit benefit) {
//                        Log.i(TAG, benefit.toString());
//                        text.setText("获得的结果为：" + benefit.toString());
//                    }
//
//                    @Override
//                    protected void _onError(String message,int code) {
////                        if(code==401){
////                            startActivity(new Intent(RetrofitActivity.this, MainActivity.class));
////                            return;
////                        }
//                        Toast.makeText(RetrofitActivity.this, message, Toast.LENGTH_LONG).show();
//                    }
//                });
//



        Observable<Benefit> fromNetwrok= Api.getDefault().rxBenefits().compose(RxHelper.<Benefit>handleResult());
        RxRetrofitCache.load(RetrofitActivity.this, "cacheKey", 10 * 60 * 60,fromNetwrok,false)
                .subscribe(new RxTest<Benefit>(RetrofitActivity.this,"加载中..."){

                    @Override
                    public void onSuccess(Benefit benefit) {
                        text.setText("获得的结果为：" + benefit.toString());
                    }

                    @Override
                    public void onFail(String message) {
                        Tools.showTextToast(mContext,message);
                    }
                });


//        Observable<ArrayList<Benefit>> fromNetwrok = Api.getDefault()
//                .rxBenefits()
//                .compose(RxHelper.<ArrayList<Benefit>>handleResult());
//        RxRetrofitCache.load(this, "cacheKey", 10 * 60 * 60, fromNetwrok, false)
//                .subscribe(new RxSubscribe<<Benefit>>(this, "正在下载福利") {
//                    @Override
//                    protected void _onNext(Benefit benefits) {
//                        Log.i(TAG, benefits.toString());
//
//                    }
//
//                    @Override
//                    protected void _onError(String message) {
//                        Toast.makeText(RetrofitActivity.this, message, Toast.LENGTH_LONG).show();
//                    }
//                });






//        RxRetrofitCache.load(this,"cacheKey",10 * 60 * 60,fromNetwrok,false)
//                .subscribe(new RxSubscribe<ArrayList<Benefit>>(this,"正在下载福利") {
//                    @Override
//                    protected void _onNext(ArrayList<Benefit> benefits) {
//                        Log.i(TAG,benefits.toString());
//                        text.setText("获得的结果为：" + benefits.toString());
//                    }
//
//                    @Override
//                    protected void _onError(String message) {
//                        Toast.makeText(RetrofitActivity.this,message,Toast.LENGTH_LONG).show();
//                    }
//                });
    }
}
